package cn.bdqn.controller;

import cn.bdqn.pojo.Employees;
import cn.bdqn.pojo.Integral;
import cn.bdqn.pojo.PageInfo;
import cn.bdqn.service.EmployeesService;
import cn.bdqn.util.EmailUtils;
import cn.bdqn.util.GenerateLinkUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 刘旻昊
 * @Date: Created in 2020/6/22  19:10
 */

//@RestController
@Controller
public class EmployeesController {
    @Resource
    EmployeesService employeesService;

    @GetMapping("/list1")
    public String list(Model model){
        List<Employees> list= employeesService.listAll();
        model.addAttribute("list",list);
        return "list1";
    }

    @RequestMapping("/index1")
    public String index(Model model){
        List<Employees> list= employeesService.listAll();
        model.addAttribute("list",list);
        return "index1";
    }
    @ResponseBody
    @RequestMapping("/delete/{id}1")
    public String del(@PathVariable("id") Integer id){
        Integer i=0;
        i= employeesService.del(id);
        return i.toString();
    }

    @RequestMapping("/toadd1")
    public String add(){
        return "hello";
    }
    @RequestMapping("/add1")
    public String adds(Employees employees, Model model){
        Integer res= employeesService.insert(employees);
        if (res>0){
            List<Employees> list= employeesService.listAll();
            model.addAttribute("list",list);
            return "index1";
        }else{
            return "hello";
        }
    }
    //转我的信息页面
    @RequestMapping("/selectByid")
    public String seleId(Model model, HttpServletRequest request){
        Integer id = (Integer) request.getSession().getAttribute("id");
        List<Employees> list= employeesService.listAllByid321(id);
        for (Employees list1:list){
            System.out.println(list1.getId());
        }
        model.addAttribute("list",list);
        return "personal/menuAdmin";
    }
    //转积分查询页面及积分详情
    @RequestMapping("/selectByid2")
    public String seleId2(Model model,HttpServletRequest request,@RequestParam(name = "pn",defaultValue = "1") Integer pn){
        //查总积分
        Integer id = (Integer) request.getSession().getAttribute("id");
        List<Employees> list= employeesService.listAllByid(id);
        model.addAttribute("list",list);

        //查积分详情及分页
        List<Integral> emps = null;
        if(pn <= 1){
            emps = employeesService.getEmps(id,0);
        }else{
            emps = employeesService.getEmps(id,(pn-1)*3);
        }
        PageInfo pageInfo = new PageInfo(pn);
        pageInfo.setTotal(employeesService.getCount(id));
        pageInfo.setIntegral(emps);
        model.addAttribute("pageInfo",pageInfo);
        request.getSession().setAttribute("integrallist",pageInfo);
        return "personal/admin";
    }
    //修改我的信息
    @RequestMapping("/upduser1")
    public String upd(Employees employees, Model model){
        Integer res= employeesService.upd(employees);
        if (res>0){
            List<Employees> list= employeesService.listAll();
            model.addAttribute("list",list);
            return "redirect:/selectByid";
        }else{
            return "redirect:/selectByid";
        }
    }
    //编辑我的信息
    @RequestMapping("/menuAdminPer")
    public String menuAdminPer(Model model,HttpServletRequest request){
        Integer id = (Integer) request.getSession().getAttribute("id");
        List<Employees> list= employeesService.listAllByid321(id);
        model.addAttribute("list",list);
        return "personal/menuAdminPer";
    }

    @GetMapping("/signScore")
    public String signScore(Employees employees, HttpServletRequest request){
        Integer id = (Integer) request.getSession().getAttribute("id");
        employeesService.updateScoer(employees);
        return "index";
    }




    //跳转到忘记密码页面
    @RequestMapping("/forgotPwdUI")
    public ModelAndView forgotPwdUI(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("errorMsg",null);
        modelAndView.setViewName("forgotPwd");
        return modelAndView;
    }
    //忘记密码
    @PostMapping("/forgotPwd")
    public ModelAndView forgotPwd(String userNameOrEmail){
        ModelAndView modelAndView = new ModelAndView();
        Employees user = employeesService.selectByUserNameOrEmail(userNameOrEmail);
        if (user == null){
            modelAndView.addObject("errorMsg",userNameOrEmail+"不匹配!");
            modelAndView.setViewName("forgotPwd");
            return modelAndView;
        }else {
            EmailUtils.sendResetPasswordEmail(user);
            modelAndView.setViewName("forgotPwd");
            modelAndView.addObject("sendMailMsg","您的申请已提交成功，请查看您的"+user.getEmail()+"邮箱。");
            return modelAndView;
        }
    }

    //修改密码
    @PostMapping("/resetPassword")
    public ModelAndView resetPassword(Integer id, Employees user, String password2){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("Msg","验证成功");
        Map<String, String> errors = new HashMap<String,String>();
        int a = 0;
        if (user.getPassword() == null || "".equals(user.getPassword())) {
            errors.put("newPassword", "新密码不能为空！");
        }else if (user.getPassword() != null && user.getPassword().length() < 3) {
            errors.put("newPassword","密码长度不能低于3位!");
        }else {
            errors.put("newPassword",null);
            a++;
        }

        if (password2 == null || "".equals(password2)) {
            errors.put("newPassword2", "确认新密码不能为空！");
        }else if (password2 != null && password2.length() < 3) {
            errors.put("newPassword2","密码长度不能低于3位!");
        }else {
            errors.put("newPassword2", null);
            a++;
        }

        if (!user.getPassword().equals(password2)) {
            errors.put("passwordError", "两次输入的密码不一致！");
        }else {
            errors.put("passwordError", null);
            a++;
        }

        if (a != 3) {
            modelAndView.addObject("errors",errors);
            modelAndView.setViewName("resetPassword");
            return modelAndView;
        }else {
            Employees user1 = employeesService.selectByPrimaryKey(id);
            user1.setPassword(user.getPassword());
            employeesService.updateByPrimaryKey(user1);
            modelAndView.setViewName("resetPasswordSuccess");
            modelAndView.addObject("sendMailMsg1","密码修改成功！请登录");
            return modelAndView;
        }
    }
    //跳转到忘记密码页面
    @RequestMapping("/resetPasswordUI")
    public ModelAndView resetPasswordUI(String name, String checkCode){
        ModelAndView modelAndView = new ModelAndView();
        Employees user = employeesService.selectByUserName(name);
        if (GenerateLinkUtils.verifyCheckcode(user,checkCode,1)){

            user.setRandomcode(null);
            employeesService.updateByPrimaryKey(user);
            modelAndView.addObject("Msg","验证成功");
            modelAndView.addObject("user",user);
            Map<String, String> errors = new HashMap<String,String>();
            errors.put("passwordError",null);
            errors.put("newPassword",null);
            errors.put("newPassword2",null);
            modelAndView.addObject("errors",errors);
            modelAndView.setViewName("resetPassword");
            return modelAndView;
        }else {
            modelAndView.addObject("Msg","验证失败");
            modelAndView.setViewName("resetPassword");
            return modelAndView;
        }
    }
}